package com.eorchis.util;

import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;

public class JsonMapperUtils {
	
	private static final Logger logger = Logger.getLogger(JsonMapperUtils.class);
	private static ObjectMapper mapper = new ObjectMapper();
	
	public static String beanToJson(Object obj) {
		try {
			return mapper.writeValueAsString(obj);
		} catch (Exception e) {
			logger.debug("Object to Json Exception : " + e.getMessage());
			throw new RuntimeException(e);
		}
	}
	
	public static <T> T jsonToBean(String jsonString,Class<T> beanClass) {
		try {
			T bean = mapper.readValue(jsonString, beanClass);
			return bean;
		} catch (Exception e) {
			logger.debug("Json to Object Exception : " + e.getMessage());
			throw new RuntimeException(e);
		}
	}
}
